import pygame

class GameState():
    """Base class for game states"""
    
    #defines values that represent no mod keys being held down
    noMods = (0, 4096, 8192, 12288)
    
    #valid states the game can be in
    validStates = {}
    
    #empty placeholders for states
    keybinds = {}       #'action': pygame key
    keybindsAdv = {}    #'action': (pygame key, (pygame mod, pygame mod))
    _actions = {}       #'action': function_to_call reference
    
    def draw(self, screen):
        pass
    
    def enter_state(self):
        """Called when state is being entered from different state. Allows for
        setting up internal variables.
        """
        pass
    
    def handle_events(self, events, mods):
        """Handles events from the passed queue
        
        :param events: events in the queue (doesn't handle pygame.QUIT event)
        :param mods: modifiers being held down at the time of the event
            occurring
        :returns: None if user requested a game exit, otherwise the GameState
            that should be active (could be self)
        """
        for event in events:
            #keybind not found yet
            found = False
            
            if event.type == pygame.KEYDOWN:
                #check advanced keybinds listing
                #confirm if event is advanced or not
                if mods not in GameState.noMods:
                    for a in self.keybindsAdv:
                        if event.key == self.keybindsAdv[a][0]:                            
                            #if each modifier is matched, run action and stop
                            #checking for match to this event 
                            modMatch = True
                            for modKey in self.keybindsAdv[a][1]:
                                modMatch = mods & modKey and modMatch
                            
                            if modMatch:    
                                self._actions[a]()
                                found = True
                                break
                            
                #if still not found, check basic keybinds listing
                if not found:
                    for a in self.keybinds:
                        if event.key == self.keybinds[a]:
                            self._actions[a]()
                            break
    
    def update(self, etime):
        return self